package com.cod.game;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.cod.game.system.GameSettings;
import com.cod.game.system.GameSystem;
import com.cod.game.system.RenderSystem;

public class GameBase extends Activity {
	protected RenderSystem renderSystem;
	protected GLSurfaceView surfaceView;
	protected GameSystem gameSystem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Mach Vollbildschirm
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// Init GameConfig
		initConfig();
		// Init GameRender Thread
		initRenderSystem();
		// Init GameSystem Thread
		initGameSystem();
		super.onCreate(savedInstanceState);
	}

	protected void initGameSystem() {
		// Game-System
		gameSystem = new GameSystem(surfaceView);
		gameSystem.start();
	}

	protected void initRenderSystem() {
		surfaceView = new GLSurfaceView(this);
		setContentView(surfaceView);
		renderSystem = new RenderSystem();
		surfaceView.setRenderer(renderSystem);
		surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
	}

	protected void initConfig() {
		GameSettings.displayWidth = getWindowManager().getDefaultDisplay()
				.getWidth();
		GameSettings.displayHeight = getWindowManager().getDefaultDisplay()
				.getHeight();
		GameSettings.aspectRatio = (float) GameSettings.displayWidth
				/ GameSettings.displayHeight;
	}


	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		return gameSystem.onKeyUp(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gameSystem.onTouchEvent(event);
	}

	@Override
	protected void onPause() {
		super.onPause();
		surfaceView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		surfaceView.onResume();
	}
}
